package com.infore.dataAudit.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.infore.dataAudit.dto.AuditDataInfo;
import com.infore.dataAudit.dto.HourData;
import com.infore.dataAudit.dto.Project;
import com.infore.dataAudit.entity.TDataAudit;
import com.infore.dataAudit.enums.AuditStatus;
import com.infore.dataAudit.vo.OriginalData;

/**
 * 实体转换类
 * @author cys
 *
 */

@Component
public class EntityMapService {
    
    
    
    public TDataAudit originalData2TDataAudit(OriginalData data) {
        TDataAudit dataAudit = new TDataAudit();
        dataAudit.setId(data.getId());
        dataAudit.setProjId(data.getProjId());
        dataAudit.setCreateTime(new Timestamp(data.getStationTime().getTime()));
        dataAudit.setProjValue(data.getProjValue());
        dataAudit.setStationMn(data.getStationMn());
        dataAudit.setStationName(data.getStationName());
        dataAudit.setProjName(data.getProjName());
        
        return dataAudit;
    }
    
    /**
     * 默认有效数据
     * 
     * @author 蔡永顺
     * @return List<TDataAudit>
     * @param oriDatas
     * @param flowId
     * @return
     * @date 2019年4月24日上午9:59:40
     */
    public List<TDataAudit> originalData2TDataAudit(List<OriginalData> oriDatas, String flowId) {
        List<TDataAudit> list = new ArrayList<>();
        TDataAudit dataAudit = null;
        for (OriginalData data : oriDatas) {
            dataAudit = this.originalData2TDataAudit(data);
            dataAudit.setFlowInstanceId(flowId);
            dataAudit.setXtAudit(AuditStatus.VALIDE.getStatusCode());
            dataAudit.setXtAuditDesc(AuditStatus.VALIDE.getReasonPhrase());
            list.add(dataAudit);
        }
        return list;
    }

    public void hourData2OriginalData(List<HourData> hourDatas, List<OriginalData> oriDatas, Map<String, Project> mapProj) {
        if(hourDatas == null) {
            return;
        }
        OriginalData oriData = null;
        Project project = null;
        Double value = null;
        for (HourData hourData : hourDatas) {
            value = hourData.getValue() == null?null:Double.valueOf(hourData.getValue());
            if(value == null)
                continue;
            oriData = new OriginalData(hourData.getId(), hourData.getMn(),
                    hourData.getDataTime(), hourData.getFactorCode(), value,
                    null, null, hourData.getFlag(), 1);
            project = mapProj.get(hourData.getFactorCode());
            if(project != null) {
                oriData.setProjName(project.getProjectName());
            }
            oriDatas.add(oriData);
        }
    }

    public AuditDataInfo dataAudit2AuditDataInfo(TDataAudit tDataAudit) {
        AuditDataInfo info = new AuditDataInfo();
        info.setCreateTime(tDataAudit.getCreateTime());
        info.setFlowInstanceId(tDataAudit.getFlowInstanceId());
        info.setId(tDataAudit.getId());
        info.setProjId(tDataAudit.getProjId());
        info.setProjValue(tDataAudit.getProjValue());
        info.setStationMn(tDataAudit.getStationMn());
        info.setStationName(tDataAudit.getStationName());
        info.setProjName(tDataAudit.getProjName());
        return info;
    }

    public List<TDataAudit> originalData2TDataAudit(List<OriginalData> oriDatas, Map<String, String> timeFlowIdMap) {
        List<TDataAudit> list = new ArrayList<>();
        TDataAudit dataAudit = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (OriginalData data : oriDatas) {
            String t = sdf.format(data.getStationTime());
            String flowId = timeFlowIdMap.get(t);
            if(StringUtils.isBlank(flowId))
                continue;
            dataAudit = this.originalData2TDataAudit(data);
            dataAudit.setFlowInstanceId(flowId);
            dataAudit.setXtAudit(AuditStatus.VALIDE.getStatusCode());
            dataAudit.setXtAuditDesc(AuditStatus.VALIDE.getReasonPhrase());
            list.add(dataAudit);
        }
        return list;
    
    }
    
    
}
